package com.hngates.project.controller;

import java.io.IOException;
import java.util.List;

import com.hngates.common.utils.ShiroUtils;
import com.hngates.project.domain.dto.SiteInformationDTO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hngates.common.annotation.Log;
import com.hngates.common.enums.BusinessType;
import com.hngates.project.domain.SiteInformation;
import com.hngates.project.service.ISiteInformationService;
import com.hngates.common.core.controller.BaseController;
import com.hngates.common.core.domain.AjaxResult;
import com.hngates.common.utils.poi.ExcelUtil;
import com.hngates.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 工地信息Controller
 *
 * @author ruoyi
 * @date 2022-06-23
 */
@Controller
@RequestMapping("/project/information")
public class SiteInformationController extends BaseController {
    private String prefix = "project/information";

    @Autowired
    private ISiteInformationService siteInformationService;

    @RequiresPermissions("project:information:view")
    @GetMapping()
    public String information() {
        return prefix + "/information";
    }

    /**
     * 查询工地信息列表
     */
    @RequiresPermissions("project:information:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SiteInformation siteInformation) {
        startPage();
        List<SiteInformation> list = siteInformationService.selectSiteInformationList(siteInformation);
        return getDataTable(list);
    }

    /**
     * 导出工地信息列表
     */
    @RequiresPermissions("project:information:export")
    @Log(title = "工地信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SiteInformation siteInformation) {
        List<SiteInformation> list = siteInformationService.selectSiteInformationList(siteInformation);
        ExcelUtil<SiteInformation> util = new ExcelUtil<SiteInformation>(SiteInformation.class);
        return util.exportExcel(list, "工地信息数据");
    }

    /**
     * 下载模板
     *
     * @return
     */
    @RequiresPermissions("project:information:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<SiteInformation> util = new ExcelUtil<>(SiteInformation.class);
        return util.importTemplateExcel("工地信息数据");
    }

    /**
     * 导入
     *
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */

    @RequiresPermissions("project:information:import")
    @PostMapping("/importInformation")
    @ResponseBody
    public AjaxResult importInformation(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<SiteInformationDTO> util = new ExcelUtil<>(SiteInformationDTO.class);
        List<SiteInformationDTO> informationList = util.importExcel(file.getInputStream(), 1);
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = siteInformationService.importInformation(informationList, updateSupport, operName);
        return AjaxResult.success(message);

    }


    /**
     * 新增工地信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存工地信息
     */
    @RequiresPermissions("project:information:add")
    @Log(title = "工地信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SiteInformation siteInformation) {
        return toAjax(siteInformationService.insertSiteInformation(siteInformation));
    }

    /**
     * 修改工地信息
     */
    @RequiresPermissions("project:information:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        SiteInformation siteInformation = siteInformationService.selectSiteInformationById(id);
        mmap.put("siteInformation", siteInformation);
        return prefix + "/edit";
    }

    /**
     * 修改保存工地信息
     */
    @RequiresPermissions("project:information:edit")
    @Log(title = "工地信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SiteInformation siteInformation) {
        return toAjax(siteInformationService.updateSiteInformation(siteInformation));
    }

    /**
     * 删除工地信息
     */
    @RequiresPermissions("project:information:remove")
    @Log(title = "工地信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(siteInformationService.deleteSiteInformationByIds(ids));
    }
}
